package com.vdf.sisconsolid.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import com.vdf.sisconsolid.rest.requestresponsedata.FilterParameter;
import com.vdf.sisconsolid.rest.requestresponsedata.ValidatedField;
import com.vdf.sisconsolid.rest.type.Staff;
import com.vdf.sisconsolid.util.UtilSC;

public class StaffController {

	public static Map<String, Object> saveAndResponse(Staff staff,
			List<Staff> staffList) {

		Map<String, Object> responseRest = validateStaff(staff);

		if (responseRest.get("success").equals(true)) {
			staffList.add(staff);
		}
		return responseRest;
	}

	public static Map<String, Object> updateAndResponse(Staff staff,
			List<Staff> staffList) {

		Map<String, Object> responseRest = validateStaff(staff);

		if (responseRest.get("success").equals(true)) {
			staffList.set(staffList.indexOf(staff), staff);
		}
		return responseRest;
	}

	private static Map<String, Object> validateStaff(Staff staff) {

		List<ValidatedField> validatedFieldList = new ArrayList<ValidatedField>();

		if (UtilSC.isEmptyString(staff.getAddress1())) {
			createValidateField("address1", "Debe ingresar una Address1",
					validatedFieldList);
		}
		if (UtilSC.isEmptyString(staff.getFirstName())) {
			createValidateField("firstName", "Debe ingresar una FirstName",
					validatedFieldList);
		}
		if (UtilSC.isEmptyString(staff.getLastName())) {
			createValidateField("lastName", "Debe ingresar una LastName",
					validatedFieldList);
		}

		if (validatedFieldList.size() > 0) {
			return getResponseRest(false, "validation",
					"Por favor corrija los siguientes errores",
					validatedFieldList);
		}
		return getResponseRest(true, "info", "Staff record saved", staff);
	}

	private static void createValidateField(String field, String message,
			List<ValidatedField> vfList) {
		ValidatedField vf = new ValidatedField();
		vf.setField(field);
		vf.setMessage(message);
		vfList.add(vf);
	}

	private static Map<String, Object> restResponset;

	public static Map<String, Object> getResponseRest(Boolean success,
			String dataType, String message, Object data) {
		return getResponseRest(success, dataType, message, data, null);
	}

	public static Map<String, Object> getResponseRest(Boolean success,
			String dataType, String message, Object data, Integer totalCount) {
		restResponset = new HashMap<String, Object>();
		restResponset.put("success", success);
		restResponset.put("type", dataType);
		restResponset.put("message", message);
		restResponset.put("data", data);
		restResponset.put("count", totalCount);
		return restResponset;
	}

	public static Map<String, Object> getResponseRest(List<Staff> staffList,
			String jsonFilters) {
		List<Staff> filteredStaffList = new ArrayList<Staff>();
		FilterParameter[] filters = (FilterParameter[])fromJson(jsonFilters, FilterParameter[].class);
		if (filters == null || filters.length == 0) {
			filteredStaffList = staffList;
		} else {
			for (FilterParameter fp : filters) {
				if ("firstName".equals(fp.getProperty())) {
					for (Staff staff : staffList) {
						if (staff.getFirstName().equals(fp.getValue())) {
							filteredStaffList.add(staff);
						}
					}
				}
				if ("position".equals(fp.getProperty())) {
					for (Staff staff : staffList) {
						if (staff.getPosition().toString()
								.equals(fp.getValue())) {
							filteredStaffList.add(staff);
						}
					}
				}
			}
		}
		return getResponseRest(true, "info", "Filtered Info",
				filteredStaffList, filteredStaffList.size());
	}

	public static Object fromJson(String json, Class clazz) {
		if (json == null) {
			return null;
		}
		Object object = null;
		try {
			object = new ObjectMapper()
					.readValue(json, FilterParameter[].class);
		} catch (JsonParseException e) {
		} catch (JsonMappingException e) {
		} catch (IOException e) {
		}
		return object;
	}

}
